package com.hupu.jrs.ui.main

import android.text.TextUtils
import com.hupu.jrs.api.forum.ForumApi
import com.hupu.jrs.api.game.GameApi
import com.hupu.jrs.components.okhttp.OkHttpHelper
import com.hupu.jrs.injector.PerActivity
import com.hupu.jrs.util.UpdateAgent
import dagger.Module
import dagger.Provides
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Func2
import rx.schedulers.Schedulers

/**
 * Created by sll on 2016/5/13.
 */
@Module
class MainModule(private val mActivity: MainActivity) {

    @Provides
    @PerActivity
    internal fun provideNotificationObservable(mGameApi: GameApi,
                                               mForumApi: ForumApi): Observable<Int> {
        return Observable.zip(mGameApi.queryPmList(""), mForumApi.getMessageList("", 1),
                Func2<Any, Any, Int> { pmData, messageData ->
                    var size = 0
                    if (pmData != null) {
                        if (pmData.is_login === 0) {
                            return@Func2 null
                        }
                        for (pm in pmData.result.data) {
                            if (!TextUtils.isEmpty(pm.unread) && pm.unread.equals("1")) {
                                size++
                            }
                        }
                    }
                    if (messageData != null && messageData.status === 200) {
                        size += messageData.result.list.size()
                    }
                    size
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
    }

    @Provides
    @PerActivity
    internal fun provideUpdateAgent(mOkHttpHelper: OkHttpHelper): UpdateAgent {
        return UpdateAgent(mOkHttpHelper, mActivity)
    }
}
